package com.doit.day01;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

public class ZkCli {
    public static void main(String[] args) throws Exception {
        /**
         * 连接字符串：
         * 超时时间：
         */
        //创建连接对象
        ZooKeeper zooKeeper = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181",2000,null);
        //用对象调用api   对象调用方法
        /*List<String> children = zooKeeper.getChildren("/doit", null);
        for (String child : children) {
            System.out.println(child);
        }*/
        getAllChild(zooKeeper,"/");
        //关闭资源
        zooKeeper.close();
    }


    //   //teacher1    /doit/teacher
    public static void getAllChild(ZooKeeper zooKeeper,String path) throws Exception, KeeperException {
        List<String> children = zooKeeper.getChildren(path, null);
        //能拿到他的子节点  ==》子节点下面是不是有东西
        if(children != null && children.size() >0){
            for (String path1 : children) {
                if (path.equals("/")){
                    getAllChild(zooKeeper,path +path1);
                    System.out.println("节点是："+path +path1);
                }else {
                    getAllChild(zooKeeper,path + "/" +path1);
                    System.out.println("节点是："+path+ "/" + path1);
                }
            }
        }
    }


}
